<template>
  <div class="app-container">
    <div class="form">
      <el-form ref="form"
               status-icon
               :model="form"
               :rules="formRules"
               auto-complete="off"
               label-width="120px">

        <el-form-item label="旧密码"
                      prop="Password">
          <el-input v-model="form.Password"
                    placeholder="旧密码"
                    prefix-icon="el-icon-edit"
                    clearable></el-input>
        </el-form-item>

        <el-form-item label="新密码"
                      prop="NewPassword">
          <el-input v-model="form.NewPassword"
                    placeholder="新密码"
                    prefix-icon="el-icon-mobile-phone"
                    clearable></el-input>
        </el-form-item>

        <el-form-item label="重复新密码"
                      prop="ConfirmPassword">
          <el-input v-model="form.ConfirmPassword"
                    placeholder="重复新密码"
                    prefix-icon="el-icon-message"
                    clearable></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary"
                     :loading="formLoading"
                     @click="submitForm">更新</el-button>
        </el-form-item>

      </el-form>
    </div>
  </div>
</template>

<script>
import { get, update, modifyPassword } from '@/api/my'

export default {
  name: 'info',
  data () {
    var validateCheckPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.form.NewPassword) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      form: {
        Password: '',
        NewPassword: '',
        ConfirmPassword: ''
      },
      formLoading: false,
      formRules: {
        Password: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
        NewPassword: [{ required: true, message: '新密码不能为空', trigger: 'blur' }],
        ConfirmPassword: [{ required: true, validator: validateCheckPass, trigger: 'blur' }]
      }
    }
  },
  created () {
  },
  methods: {
    submitForm () {
      let form = this.form
      this.formLoading = true
      this.$refs.form.validate((valid) => {
        if (valid) {
          modifyPassword(form).then(response => {
            this.$message({
              message: '更新成功',
              type: 'success'
            })
            this.formLoading = false
          })
        } else {
          this.formLoading = false
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
.form {
  width: 420px;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
